﻿///
///搜索属性模块处理
///
(function ($) {
    $.fn.searchPropHandler = function (options) {
        var $this = this;
        if ($this.data('searchPropHandler')) {
            return $this.data('searchPropHandler');
        }

        $this.data('searchPropHandler', $this);

        $this.setting = $.extend({},
        //默认配置
        {
        //分值数据
        glist: [],

        //选中
        sellected: function () {
        },

        //分组模板
        groupTemplate: '<div data-role="collapsible" sp_group="{id}">' +
                                '<h2><font>{name}</font><span>{全部}</span></h2>' +
                                 '<fieldset data-role="controlgroup" data-iconpos="right">{itemlist}</fieldset>' +
                            '</div>',
        //单选模板
        radioItemTemplate: '<input sp_item="{id}" name="sp_{gid}" id="sp_item_{id}" value="{id}" type="radio">' +
                            '<label for="sp_item_{id}">{name}</label>',

        //多选模板
        checkboxItemTemplate: '<input sp_item="{id}" name="sp_{gid}" id="sp_item_{id}" value="{id}" type="checkbox">' +
                            '<label for="sp_item_{id}">{name}</label>'
    },
        options);

    //分组/子项数据
    $this.glist = options.glist;

    if ($this.glist.length == 0) {
        //return;
    }

    //输出html
    $($this.glist).each(function () {
        var group = this;
        var itemhtml = '';
        if (group.Group.Kind != 'M') {
            itemhtml = $this.setting.radioItemTemplate.format({ gid: group.Group.Id, name: '全部', id: '-1' });
        }
        $(group.List).each(function () {
            var itemTemp = '';
            if (group.Group.Kind == 'M') {
                itemTemp = $this.setting.checkboxItemTemplate;
            } else {
                itemTemp = $this.setting.radioItemTemplate;
            }
            itemhtml += itemTemp.format({ gid: this.SearchPropertyId, name: this.PropertyName, id: this.Id });
        });
        $this.append($this.setting.groupTemplate.format({ itemlist: itemhtml, name: group.Group.PropertyValue, id: group.Group.Id }));
    });
    $this.trigger('create');
    $('input[sp_item]', $this).change(function () {
        var item = getItem($(this).val());
        var group = getGroup($(this).parents('[sp_group]').attr('sp_group'));
        if (group.Group.Kind == 'M') {//多选
            //设置选中状态
            item.selected = $(this).prop('checked');

            //显示选中文本
            var text = '';
            $(group.List).each(function () {
                if (this.selected == true) {
                    text += this.PropertyName + ",";
                }
            });
            if (text == '') {
                text = '全部';
            } else {
                text = text.substr(0, text.length - 1);
            }
            $(".selected_text", $(this).parents('[sp_group]')).text(text);
        } else {//单选
            //取消选中
            $(group.List).each(function () {
                this.selected = false;
            });
            //选中此项
            if (item) {
                item.selected = true;
                //显示选中文本
                $(".selected_text", $(this).parents('[sp_group]')).text(item.PropertyName);
            } else {
                $(".selected_text", $(this).parents('[sp_group]')).text('全部');
            }
        }

    });

    var getGroup = function (groupId) {
        var result = false;
        $($this.glist).each(function () {
            var group = this;
            if (group.Group.Id == groupId) {
                result = group;
                return false;
            }
        });
        return result;
    }

    var getItem = function (itemId) {
        var result = false;
        $($this.glist).each(function () {
            var group = this;
            $(group.List).each(function () {
                var item = this;
                if (item.Id == itemId) {
                    result = item;
                    return false;
                }
            });
            if (result) {
                return false;
            }
        });
        return result;
    }

    $this.clearSelected = function () {
        $($this.glist).each(function () {
            var group = this;
            $(group.List).each(function () {
                this.selected = false;
            });
            $(".selected_text", $('[sp_group=' + group.Group.Id + ']', $this)).text('全部');
        });
        $('input[sp_item]').prop("checked", false).checkboxradio("refresh");
    }

    $this.getResult = function () {
        var resultObj = {};
        $($this.glist).each(function () {
            var group = this;
            $(group.List).each(function () {
                var item = this;
                if (item.selected == true) {
                    if (!resultObj[group.Group.Id]) {
                        resultObj[group.Group.Id] = '';
                    }
                    resultObj[group.Group.Id] += item.Id + ",";
                }
            });
        });

        var resultArr = [];
        //去除末尾','
        for (var prop in resultObj) {
            resultObj[prop] = resultObj[prop].substr(0, resultObj[prop].length - 1);
            resultArr.push({ Key: prop, Value: resultObj[prop] });
        }

        return resultArr;
    }
}
})(jQuery);